package com.geek.water.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.geek.water.entity.Menu;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;

import java.util.List;

/**
 * @author chenmin
 * @desc 用户权限查询Mapper
 * @date 2025/01/08
 */
public interface UserPermissionMapper extends BaseMapper<Menu> {

    /**
     * 根据用户ID查询用户通过基础角色获得的权限ID（不包括专属权限角色）
     * @param userId 用户ID
     * @return 权限ID列表
     */
    @Select("SELECT DISTINCT p.id " +
            "FROM user_role ur " +
            "JOIN role_permission rp ON ur.role_id = rp.role_id " +
            "JOIN permission p ON rp.permission_id = p.id " +
            "WHERE ur.user_id = #{userId} " +
            "AND ur.role_id NOT IN (SELECT id FROM role WHERE name LIKE 'USER_%_CUSTOM')")
    List<Integer> selectUserBaseRolePermissionIds(@Param("userId") Long userId);

    /**
     * 根据用户ID查询用户通过专属权限角色获得的权限ID
     * @param userId 用户ID
     * @return 权限ID列表
     */
    @Select("SELECT DISTINCT p.id " +
            "FROM user_role ur " +
            "JOIN role_permission rp ON ur.role_id = rp.role_id " +
            "JOIN permission p ON rp.permission_id = p.id " +
            "WHERE ur.user_id = #{userId} " +
            "AND ur.role_id IN (SELECT id FROM role WHERE name LIKE 'USER_%_CUSTOM')")
    List<Integer> selectUserCustomPermissionIds(@Param("userId") Long userId);

    /**
     * 根据用户ID查询用户拥有的所有权限ID（通过角色关联，兼容旧版本）
     * @param userId 用户ID
     * @return 权限ID列表
     */
    @Select("SELECT DISTINCT p.id " +
            "FROM user_role ur " +
            "JOIN role_permission rp ON ur.role_id = rp.role_id " +
            "JOIN permission p ON rp.permission_id = p.id " +
            "WHERE ur.user_id = #{userId}")
    List<Integer> selectUserPermissionIds(@Param("userId") Long userId);

    /**
     * 根据用户ID查询用户拥有的所有菜单权限ID（如果menu表存在）
     * @param userId 用户ID
     * @return 菜单ID列表
     */
    @Select("SELECT DISTINCT m.id " +
            "FROM user_role ur " +
            "JOIN role_menu rm ON ur.role_id = rm.role_id " +
            "JOIN menu m ON rm.menu_id = m.id " +
            "WHERE ur.user_id = #{userId}")
    List<Integer> selectUserMenuIds(@Param("userId") Long userId);

    /**
     * 根据用户ID查询用户拥有的所有菜单权限详情
     * @param userId 用户ID
     * @return 菜单列表
     */
    @Select("SELECT DISTINCT m.* " +
            "FROM user_role ur " +
            "JOIN role_menu rm ON ur.role_id = rm.role_id " +
            "JOIN menu m ON rm.menu_id = m.id " +
            "WHERE ur.user_id = #{userId} " +
            "ORDER BY m.priority")
    List<Menu> selectUserMenus(@Param("userId") Long userId);

    /**
     * 根据用户ID和菜单类型查询权限
     * @param userId 用户ID
     * @param level 菜单层级（1-一级菜单，2-二级菜单）
     * @return 菜单ID列表
     */
    @Select("SELECT DISTINCT m.id " +
            "FROM user_role ur " +
            "JOIN role_menu rm ON ur.role_id = rm.role_id " +
            "JOIN menu m ON rm.menu_id = m.id " +
            "WHERE ur.user_id = #{userId} AND m.level = #{level} " +
            "ORDER BY m.priority")
    List<Integer> selectUserMenuIdsByLevel(@Param("userId") Long userId, @Param("level") Integer level);

    /**
     * 删除角色的所有权限
     * @param roleId 角色ID
     * @return 影响行数
     */
    @Delete("DELETE FROM role_permission WHERE role_id = #{roleId}")
    int deleteRolePermissions(@Param("roleId") Long roleId);

    /**
     * 批量插入角色权限
     * @param roleId 角色ID
     * @param permissionIds 权限ID列表
     * @return 影响行数
     */
    @Insert("<script>" +
            "INSERT INTO role_permission (role_id, permission_id) VALUES " +
            "<foreach collection='permissionIds' item='permissionId' separator=','>" +
            "(#{roleId}, #{permissionId})" +
            "</foreach>" +
            "</script>")
    int insertRolePermissions(@Param("roleId") Long roleId, @Param("permissionIds") List<Integer> permissionIds);
}
